<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Session Time Ratio</title>
    <script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script>
    <style>
        body, html {
            width: 100%;
            height: 100%;
            margin: 0;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        #main {
            width: 600px;
            height: 400px;
        }
    </style>
</head>
<body>
<div id="main"></div>
<script type="text/javascript">
    var chartDom = document.getElementById('main');
    var myChart = echarts.init(chartDom);
    var option;

    fetch('/api/session_time_ratio/data')
        .then(response => response.json())
        .then(data => {
            console.log(data); // 打印数据到控制台

            if (data.length === 0) {
                document.getElementById('main').innerText = 'No data available';
                return;
            }

            var sessionData = data[0];
            var transformedData = [
                { name: 'Visit Length 1s-3s Ratio', value: sessionData.visitLength1s3sRatio },
                { name: 'Visit Length 4s-6s Ratio', value: sessionData.visitLength4s6sRatio },
                { name: 'Visit Length 7s-9s Ratio', value: sessionData.visitLength7s9sRatio },
                { name: 'Visit Length 10s-30s Ratio', value: sessionData.visitLength10s30sRatio },
                { name: 'Visit Length 30s-60s Ratio', value: sessionData.visitLength30s60sRatio },
                { name: 'Visit Length 1m-3m Ratio', value: sessionData.visitLength1m3mRatio },
                { name: 'Visit Length 3m-10m Ratio', value: sessionData.visitLength3m10mRatio },
                { name: 'Visit Length 10m-30m Ratio', value: sessionData.visitLength10m30mRatio },
                { name: 'Visit Length 30m+ Ratio', value: sessionData.visitLength30mRatio }
            ];

            option = {
                title: {
                    text: 'Session Time Ratio'
                },
                tooltip: {
                    trigger: 'item'
                },
                legend: {
                    top: '5%',
                    left: 'center'
                },
                series: [
                    {
                        name: 'Session Time Ratio',
                        type: 'pie',
                        radius: '50%',
                        data: transformedData,
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };

            myChart.setOption(option);
        })
        .catch(error => {
            console.error('Error fetching data:', error);
        });
</script>
</body>
</html>
